package ag.rpg;

import java.util.ArrayList;
import java.util.List;

import ag.*;
import android.graphics.Point;
import android.graphics.PointF;
import android.graphics.RectF;

public class AGRPGMap extends AGObject {
	private byte[] _foregrounds = {
			2,2,2,2,2,2,2,2,2,2,1,1,1,1,1,1,1,1,1,1,
			2,2,2,2,2,2,2,2,2,2,1,1,1,1,1,1,1,1,1,1,
			2,2,2,2,2,2,2,2,2,2,1,1,1,1,1,1,1,1,1,1,
			2,2,2,2,2,2,2,2,2,2,1,1,1,1,1,1,1,1,1,1,
			2,2,2,2,2,2,2,2,2,2,1,1,1,1,1,1,1,1,1,1,
			2,2,2,2,2,2,2,2,2,2,1,1,1,1,1,1,1,1,1,1,
			2,2,2,2,2,2,2,2,2,2,1,1,1,1,1,1,1,1,1,1,
			2,2,2,2,2,2,2,2,2,2,1,1,1,1,1,1,1,1,1,1,
			2,2,2,2,2,2,2,2,2,2,1,1,1,1,1,1,1,1,1,1,
			2,2,2,2,2,2,2,2,2,2,1,1,1,1,1,1,1,1,1,1,
	};
	private byte[] _backgrounds = {
		0,0,0,0,0,0,0,0,0,0,2,2,2,2,2,0,0,0,0,0,
		0,0,0,0,0,0,0,0,0,0,2,0,0,0,0,0,0,0,0,0,
		0,0,0,0,0,0,0,0,0,0,2,0,0,0,0,0,0,0,0,0,
		0,0,0,0,0,0,0,0,0,0,2,0,0,0,0,0,0,0,0,0,
		0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
		0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
		0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,0,
		0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,0,
		0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,0,
		0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,0,
	};
	private byte[] _tileids = {
			0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,
			0,0,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0,0,0,0,0,0,0,
			0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
			0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
			0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
			0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
			0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
	};
	private final static int TILE_WIDTH = 96;
	public final static int TILE_HEIGHT = 64;
	private int _row;
	private int _col;
	private List<AGRPGMapCell> _mapData;
	private AGModel _groundModel1;
	private AGModel _groundModel2;
	private AGModel _groundModel12;
	private AGTexture _texTile;
	
	private PointF _originPoint;
	
	public void setMapSize(int row, int col, AGModel groundModel){
		_originPoint = new PointF(0, 320);
		this._row = 10;
		this._col = 20;
		this._mapData = new ArrayList<AGRPGMapCell>();
		for(int r =0; r<this._row; r++){
			for(int c = 0; c< this._col; c++){
				AGRPGMapCell cell = new AGRPGMapCell(_foregrounds[r*this._col+c],
						_backgrounds[r*this._col+c],
						_tileids[r*this._col+c],
						_tiledirs[r*this._col+c],
						_tileindexs[r*this._col+c]);
				this._mapData.add(cell);
			}
		}
		this._texTile = AGResource.getInstance().getTexture("1001/1001-00-00-00");
		
		//this._groundModel = groundModel;
		this._groundModel1 = AGModelManager.getInstance().getModel(1000);
		this._groundModel2 = AGModelManager.getInstance().getModel(1002);
		this._groundModel12 = AGModelManager.getInstance().getModel(1001);
	}

	@Override
	public void onRender(AGContext context){
		for(int r =0; r<this._row; r++){
			for(int c = 0; c< this._col; c++){
				PointF tilePos = ffxyToXY(new PointF(0,320), r, c);
				RectF destRect = new RectF(tilePos.x, tilePos.y, tilePos.x + TILE_WIDTH, tilePos.y + TILE_HEIGHT);
				AGRPGMapCell cell = this._mapData.get(r* this._col+c);
				if(cell.foregroundId == 1 && cell.backgroundId == 0){
					AGModelFrame frame = this._groundModel1.getAction(cell.tileId).getDirection(cell.tileDir).getFrame(cell.tileIndex);
					context.getGDI().drawTexture(destRect, frame.getTexture(), frame.getSrcRect());
				}else if(cell.foregroundId == 2 && cell.backgroundId == 0){
					AGModelFrame frame = this._groundModel2.getAction(cell.tileId).getDirection(cell.tileDir).getFrame(cell.tileIndex);
					context.getGDI().drawTexture(destRect, frame.getTexture(), frame.getSrcRect());
				}else{
					AGModelFrame frame = this._groundModel12.getAction(cell.tileId).getDirection(cell.tileDir).getFrame(cell.tileIndex);
					context.getGDI().drawTexture(destRect, frame.getTexture(), frame.getSrcRect());
				}
				//context.getGDI().drawTexture(destRect, this._texTile);
			}
		}
	}
	
	private Point xyToFFXY(float x, float y){
		//COL=int(x/TileW - y/TileH)
		//ROW=int(x/TileW + y/TileH)
		int col = (int)(x /TILE_WIDTH - y / TILE_HEIGHT);
		int row = (int)(x /TILE_WIDTH + y / TILE_HEIGHT);
		return new Point(row, col);
	}
	
	private PointF ffxyToXY(PointF originPoint, int row, int col){
		return new PointF(originPoint.x + (float)TILE_WIDTH / 2 *(row+col),
				originPoint.y + (float)TILE_HEIGHT / 2 *(row-col -1));
	}
	
	public PointF rcToXYCenter(int row, int col){
		return new PointF(this._originPoint.x + (float)TILE_WIDTH / 2 *(row+col + 1),
				this._originPoint.y + (float)TILE_HEIGHT / 2 *(row-col));
	}
}
